package com.bitbits.plugins.settings;

import com.bitbits.plugins.utils.NumberUtils;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import lombok.Data;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * 插件主配置文件
 */
@Data
@State(
        name = "Px2rpxSettings",
        storages = {
                @Storage("Px2rpxSettings.xml")
        }
)
public class Px2rpxSettings implements PersistentStateComponent<Px2rpxSettings> {

    // 设计图基准宽度
    public Integer designWidth = 1920;

    public static Px2rpxSettings getInstance() {
        return ServiceManager.getService(Px2rpxSettings.class);
    }

    @Nullable
    @Override
    public Px2rpxSettings getState() {
        return this;
    }

    @Override
    public void loadState(@NotNull Px2rpxSettings configuration) {
        XmlSerializerUtil.copyBean(configuration, this);
    }

    public float getRatio() {
        return NumberUtils.toScale(this.designWidth / 750f, 2);
    }
}
